package io.chenglicun.modules.clc.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.modules.clc.dao.ClcUserArchiveDao;
import io.chenglicun.modules.clc.dto.ClcUserArchiveDTO;
import io.chenglicun.modules.clc.entity.ClcUserArchiveEntity;
import io.chenglicun.modules.clc.service.ClcUserArchiveService;
import io.chenglicun.modules.sys.dao.SysDictDataDao;
import io.chenglicun.modules.sys.entity.SysDictDataEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 村民档案
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-10-30
 */
@Service
public class ClcUserArchiveServiceImpl extends CrudServiceImpl<ClcUserArchiveDao, ClcUserArchiveEntity, ClcUserArchiveDTO> implements ClcUserArchiveService {

    @Autowired
    private SysDictDataDao dictDataDao;


    @Override
    public QueryWrapper<ClcUserArchiveEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcUserArchiveEntity> wrapper = new QueryWrapper<>();

        Long userId = (Long) params.get("userId");
        wrapper.eq(ObjectUtil.isNotEmpty(userId), "user_id", userId);

        //排序字段
        String orderField = (String)params.get(Constant.ORDER_FIELD);
        String order = (String)params.get(Constant.ORDER);
        //排序
        if(StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)){
            if(Constant.ASC.equalsIgnoreCase(order)) {
                wrapper.orderByAsc(orderField);
            }else {
                wrapper.orderByDesc(orderField);
            }
        }

        return wrapper;
    }

    @Override
    public PageData<ClcUserArchiveDTO> page(Map<String, Object> params) {
        PageData<ClcUserArchiveDTO> page = super.page(params);
        setData(page.getList().toArray(new ClcUserArchiveDTO[0]));
        return page;
    }

    @Override
    public List<ClcUserArchiveDTO> list(Map<String, Object> params) {
        List<ClcUserArchiveDTO> list = super.list(params);
        setData(list.toArray(new ClcUserArchiveDTO[0]));
        return list;
    }

    @Override
    public ClcUserArchiveDTO get(Long id) {
        ClcUserArchiveDTO dto = super.get(id);
        setData(dto);
        return dto;
    }

    private void setData(ClcUserArchiveDTO... dtos) {
        List<Long> archiveType = Arrays.stream(dtos).map(ClcUserArchiveDTO::getArchiveType).distinct().collect(Collectors.toList());
        List<SysDictDataEntity> dictDataEntities = dictDataDao.selectList(new QueryWrapper<SysDictDataEntity>()
                .lambda().in(SysDictDataEntity::getId, archiveType));
        for (ClcUserArchiveDTO dto : dtos) {
            SysDictDataEntity dictDataEntity = dictDataEntities.stream().filter(t -> t.getId().equals(dto.getArchiveType())).findFirst().orElse(null);
            dto.setArchiveTypeName(dictDataEntity == null ? null : dictDataEntity.getDictLabel());
        }
    }
}